/**
 * 酒店民宿信息JS
 * @author Assassin
 */
$(document).ready(function(){
    $('#uploadPictureLink').on('click',function(){
        $('#pictureFile').click();
    });
    $('#pictureImg').on('click',function(){
        $('#pictureFile').click();
    });
});

// 上传缩略图片
function uploadPicture(){
    $.ajaxFileUpload({
        url:shiro.baseUrl + "uploadPicture",
        secureuri:false,
        fileElementId:"pictureFile",
        dataType: 'json',
        success: function (data) {
            $("#pictureImg").attr("src", data.preUrl + "?v=" + new Date());
            $('#picture').val(data.picture);
        },error:function(XMLHttpRequest, textStatus, errorThrown){
            layer.msg(errorThrown, {icon: 5,time: 1000});
        }
    });
}

// 执行添加酒店民宿信息
function excuteAddHotelInfo() {
    var title = $.trim($('#title').val());
    if(shiro.util.isEmpty(title)) {
        layer.tips('酒店民宿标题不能为空', '#title');
        return false;
    }
    var desc = $.trim($('#desc').val());
    if(shiro.util.isEmpty(desc)) {
        layer.tips('酒店民宿简介不能为空', '#desc');
        return false;
    }
    var feature = $.trim($('#feature').val());
    if(shiro.util.isEmpty(feature)) {
        layer.tips('酒店民宿特征不能为空', '#feature');
        return false;
    }
    var address = $.trim($('#address').val());
    if(shiro.util.isEmpty(address)) {
        layer.tips('酒店民宿位置不能为空', '#address');
        return false;
    }
    var type = $.trim($('#type').val());
    // var lng = $.trim($('#lng').val());
    // if(shiro.util.isEmpty(lng)) {
    //     layer.tips('酒店民宿经度不能为空', '#lng');
    //     return false;
    // }
    // var lat = $.trim($('#lat').val());
    // if(shiro.util.isEmpty(lat)) {
    //     layer.tips('酒店民宿纬度不能为空', '#lat');
    //     return false;
    // }
    var content = CKEDITOR.instances.content_editor.getData();
    if(shiro.util.isEmpty(content)) {
        layer.msg('酒店民宿内容不能为空', {icon: 5,time: 1000});
        return false;
    }
    var picture = $('#picture').val();
    if(shiro.util.isEmpty(picture)) {
        layer.msg('缩略图片不能为空', {icon: 5,time: 1000});
        return false;
    }
    // 提交信息
    shiro.ajax({
        url: 'hotel/executeAddHotel',
        data:{'title': title, 'type':type,'feature':feature,'address':address, "content":content,'desc':desc,"picture":picture},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "hotel/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#title').val('');
            }
        }
    });
    return false;
}

// 执行修改酒店民宿信息
function excuteEditHotelInfo() {
    var title = $.trim($('#title').val());
    if(shiro.util.isEmpty(title)) {
        layer.tips('酒店民宿标题不能为空', '#title');
        return false;
    }
    var desc = $.trim($('#desc').val());
    if(shiro.util.isEmpty(desc)) {
        layer.tips('酒店民宿简介不能为空', '#desc');
        return false;
    }
    var feature = $.trim($('#feature').val());
    if(shiro.util.isEmpty(feature)) {
        layer.tips('酒店民宿特征不能为空', '#feature');
        return false;
    }
    var address = $.trim($('#address').val());
    if(shiro.util.isEmpty(address)) {
        layer.tips('酒店民宿位置不能为空', '#address');
        return false;
    }
    var type = $.trim($('#type').val());
    // var lng = $.trim($('#lng').val());
    // if(shiro.util.isEmpty(lng)) {
    //     layer.tips('酒店民宿经度不能为空', '#lng');
    //     return false;
    // }
    // var lat = $.trim($('#lat').val());
    // if(shiro.util.isEmpty(lat)) {
    //     layer.tips('酒店民宿纬度不能为空', '#lat');
    //     return false;
    // }
    var content = CKEDITOR.instances.content_editor.getData();
    if(shiro.util.isEmpty(content)) {
        layer.msg('酒店民宿内容不能为空', {icon: 5,time: 1000});
        return false;
    }
    var picture = $('#picture').val();
    if(shiro.util.isEmpty(picture)) {
        layer.msg('缩略图片不能为空', {icon: 5,time: 1000});
        return false;
    }
    var hotelId = $('#hotelId').val();
    // 提交信息
    shiro.ajax({
        url: 'hotel/executeEditHotel',
        data:{'hotelId': hotelId,'type':type, 'title': title,'feature':feature,'address':address, "content":content,'desc':desc, "picture":picture},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "hotel/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#title').val('');
            }
        }
    });
    return false;
}

// 删除酒店民宿
function deleteHotel(hotelId) {
    layer.msg('确定删除该酒店民宿？', {
        time: 0,
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            // 提交删除
            shiro.ajax({
                url: 'hotel/delete',
                data:{"hotelId": hotelId},
                success: function(data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            window.location.href = shiro.baseUrl + "hotel/list";
                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1000});
                    }
                }
            });
        }
    });
}

// 批量删除酒店民宿
function batchDeleteHotels() {
    var hotelIdArray = shiro.form.getCheckBoxSelectValues('hotelIdCheckBox');
    if(hotelIdArray.length == 0) {
        layer.msg('请选择要操作的数据行', {icon: 5,time: 1000});
    } else {
        layer.msg('确定删除选中的酒店民宿？', {
            time: 0,
            btn: ['确定', '取消'],
            yes: function(index){
                layer.close(index);
                var hotelIds = hotelIdArray.join(',');
                // 提交删除
                shiro.ajax({
                    url: 'hotel/batchDelete',
                    data:{"hotelIds": hotelIds},
                    success: function(data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1000}, function() {
                                window.location.href = shiro.baseUrl + "hotel/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1000});
                        }
                    }
                });
            }
        });
    }
}


//通过酒店民宿
function adoptHotel(hotelId) {
    layer.msg('确定删除该酒店民宿？', {
        time: 0,
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            // 提交删除
            shiro.ajax({
                url: 'hotel/adoptRestaurant',
                data:{"hotelId": hotelId},
                success: function(data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            window.location.href = shiro.baseUrl + "hotel/listNo";
                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1000});
                    }
                }
            });
        }
    });
}

/**
 * 执行驳回
 */
function rejectHotel(hotelId){
	layer.open({
		type: 2,
		area: ['400px', '230px'],
		title: '填写驳回原因',
		content: shiro.baseUrl + 'hotel/rejectHotel?hotelId='+hotelId
	});
}


/**
 * 更新驳回原因
 */
function updateReason(){
	var hotelId = $.trim($('#hotelId').val());
	var reason = $.trim($('#reason').val());
	if(shiro.util.isEmpty(reason)) {
        layer.msg('驳回原因不能为空', {icon: 5,time: 1000});
        return false;
    }
	 // 提交信息
    shiro.ajax({
        url: 'hotel/updateReason',
        data:{"hotelId":hotelId,"reason":reason},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                	parent.layer.closeAll();
                	parent.window.location.href = shiro.baseUrl + "hotel/listNo";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
            }
        }
    });
    return false;
}


function viewHotel(hotelId){
	layer.open({
		type: 2,
		area: ['400px', '230px'],
		title: '查看驳回原因',
		content: shiro.baseUrl + 'hotel/viewHotel?hotelId='+hotelId
	});
}



